package net.kosev.battleships;

import net.kosev.battleships.model.Competitor;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.admob.android.ads.AdManager;

/**
 * Results Activity
 * 
 * @author Nikola
 *
 */
public class ResultScreen extends Activity {

	/**
	 * Cancel result
	 */
	public static final int RESULT_CANCEL = 0;
	
	/**
	 * Image component
	 */
	private ImageView mImage = null;
	
	/**
	 * Continue button
	 */
	private Button mContinue = null;
	
	/**
	 * 
	 */
	private View mPlayerBarMisses = null;
	
	/**
	 * 
	 */
	private View mPlayerBarHits = null;
	
	/**
	 * 
	 */
	private TextView mPlayerLabelMoves = null;
	
	/**
	 * 
	 */
	private TextView mPlayerLabelMisses = null;
	
	/**
	 * 
	 */
	private TextView mPlayerLabelHits = null;
	
	/**
	 * 
	 */
	private View mEnemyBarMisses = null;
	
	/**
	 * 
	 */
	private View mEnemyBarHits = null;
	
	/**
	 * 
	 */
	private TextView mEnemyLabelMoves = null;
	
	/**
	 * 
	 */
	private TextView mEnemyLabelMisses = null;
	
	/**
	 * 
	 */
	private TextView mEnemyLabelHits = null;
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);
		
		mContinue = (Button) findViewById(R.id.ButtonResult);
		mContinue.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		
		mImage = (ImageView) findViewById(R.id.ImageResult);
		int winner = GameScreen.mGame.getWinner();
		
		if (winner == Competitor.PLAYER) {
			mImage.setImageResource(R.drawable.win);
		} else {
			mImage.setImageResource(R.drawable.lose);
		}
		
		mPlayerBarMisses = findViewById(R.id.ImageYMisses);
		mPlayerBarHits = findViewById(R.id.ImageYHits);
		
		mPlayerLabelMoves = (TextView) findViewById(R.id.LabelYMoves);
		mPlayerLabelMisses = (TextView) findViewById(R.id.LabelYMisses);
		mPlayerLabelHits = (TextView) findViewById(R.id.LabelYHits);
		
		mEnemyBarMisses = findViewById(R.id.ImageEMisses);
		mEnemyBarHits = findViewById(R.id.ImageEHits);
		
		mEnemyLabelMoves = (TextView) findViewById(R.id.LabelEMoves);
		mEnemyLabelMisses = (TextView) findViewById(R.id.LabelEMisses);
		mEnemyLabelHits = (TextView) findViewById(R.id.LabelEHits);
		
		final int min = Math.round(Utils.conv(this, 5));
		
		final int playerMoves = GameScreen.mGame.getEnemyBoard().getCountMoves();
		final int playerHits = GameScreen.mGame.getEnemyBoard().getCountHits();
		final int playerMisses = playerMoves - playerHits;
		
		mPlayerLabelMoves.setText(playerMoves + "x fire");
		mPlayerLabelMisses.setText(playerMisses + "x miss");
		mPlayerLabelHits.setText(playerHits + "x hit");
		
		RelativeLayout.LayoutParams pPMiss = (RelativeLayout.LayoutParams) mPlayerBarMisses.getLayoutParams();
		pPMiss.width = Math.round(Utils.conv(this, 160 * playerMisses / playerMoves));
		if (pPMiss.width < min) {
			pPMiss.width = min;
		}
		mPlayerBarMisses.setLayoutParams(pPMiss);
		
		RelativeLayout.LayoutParams pPHit = (RelativeLayout.LayoutParams) mPlayerBarHits.getLayoutParams();
		pPHit.width = Math.round(Utils.conv(this, 160 * playerHits / playerMoves));
		if (pPHit.width < min) {
			pPHit.width = min;
		}
		mPlayerBarHits.setLayoutParams(pPHit);
		
		
		final int enemyMoves = GameScreen.mGame.getPlayerBoard().getCountMoves();
		final int enemyHits = GameScreen.mGame.getPlayerBoard().getCountHits();
		final int enemyMisses = enemyMoves - enemyHits;
		
		mEnemyLabelMoves.setText(enemyMoves + "x fire");
		mEnemyLabelMisses.setText(enemyMisses + "x miss");
		mEnemyLabelHits.setText(enemyHits + "x hit");
		
		RelativeLayout.LayoutParams pEMiss = (RelativeLayout.LayoutParams) mEnemyBarMisses.getLayoutParams();
		pEMiss.width = Math.round(Utils.conv(this, 160 * enemyMisses / enemyMoves));
		if (pEMiss.width < min) {
			pEMiss.width = min;
		}
		mEnemyBarMisses.setLayoutParams(pEMiss);
		
		RelativeLayout.LayoutParams pEHit = (RelativeLayout.LayoutParams) mEnemyBarHits.getLayoutParams();
		pEHit.width = Math.round(Utils.conv(this, 160 * enemyHits / enemyMoves));
		if (pEHit.width < min) {
			pEHit.width = min;
		}
		mEnemyBarHits.setLayoutParams(pEHit);
		
		AdManager.setTestDevices( new String[] {
			AdManager.TEST_EMULATOR
        });
	}
	
	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			setResult(RESULT_CANCEL);
			finish();
	        return true;
	    }
		return super.onKeyDown(keyCode, event);
	}

}
